﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System.Web.Mvc;

using Microsoft.Practices.Unity;

using eCRAM.Model;
using eCRAM.Dao;
using eCRAM.Bll;
using System.Configuration;
using eCRAM.WebSite.Services;
using eCRAM.WebSite.Models;
using System.Data.Linq;

namespace eCram.IoC
{
    /// <summary>
    /// Initializes the repositories, part of the IoC pattern.
    /// </summary>
    /// <see cref="IRepository{T}"/>
    public static class Bootstrapper
    {
        /// <summary>
        /// Registration of the repositories via the Unity framework.
        /// </summary>
        /// <see cref="IRepository{T}"/>
        public static void ConfigureUnityContainer()
        {
            DataContext dataContent = new DataContext(ConfigurationManager.ConnectionStrings["eCRAM"].ConnectionString);
            // Unity framework
            IUnityContainer container = new UnityContainer();

            // Repositories
            container.RegisterType<IRepository<User>, UserRepository>
                (new HttpContextLifetimeManager<UserRepository>(),
                new InjectionConstructor(dataContent));

            container.RegisterType<IRepository<CramLine>, CramLineRepository>
               (new HttpContextLifetimeManager<CramLineRepository>(),
               new InjectionConstructor(dataContent));
            
            container.RegisterType<IActivityRepository, ActivityRepository>
                (new HttpContextLifetimeManager<IActivityRepository>(),
                new InjectionConstructor(dataContent));

            container.RegisterType<ICramRepository, CramRepository>
                (new HttpContextLifetimeManager<CramRepository>(),
                new InjectionConstructor(dataContent),  new InjectionProperty("CramLineRepo"));
            
            //Bll
            container.RegisterType<IUserManager, UserManager>(new HttpContextLifetimeManager<IUserManager>(),
                new InjectionProperty("UserRepository"));
            container.RegisterType<IActivityManager, ActivityManager>(new HttpContextLifetimeManager<IActivityManager>(),
                new InjectionProperty("ActivityRepository"));
            
            container.RegisterType<ICramManager, CramManager>(new HttpContextLifetimeManager<ICramManager>(),
               new InjectionProperty("CramRepository"), new InjectionProperty("CramLineRepository"));
           
            /*
             // Repositories
             container.RegisterType<IRepository<User>, UserRepository>
                 (new HttpContextLifetimeManager<UserRepository>());

             container.RegisterType<IRepository<Activity>, ActivityRepository>
                 (new HttpContextLifetimeManager<ActivityRepository>());

             container.RegisterType<IRepository<CramLine>, CramLineRepository>
                 (new HttpContextLifetimeManager<CramLineRepository>());

             container.RegisterType<IRepository<Cram>, CramRepository>
                 (new HttpContextLifetimeManager<CramRepository>());

             //container.RegisterType<IRepository<Cram>, CramRepository>(
              *  * */
             
            // Services
            container.RegisterType<IAuthenticationService, AuthenticationService>
                (new HttpContextLifetimeManager<IAuthenticationService>());

            container.RegisterType<IFormsAuthenticationService, FormsAuthenticationService>
                (new HttpContextLifetimeManager<IFormsAuthenticationService>());

            container.RegisterType<ISession, CramSession>
                (new HttpContextLifetimeManager<ISession>());
            
            // Controller Factory
            ControllerBuilder.Current.SetControllerFactory(
                new UnityControllerFactory(container));
            
        }
    }
}
